Asciidoctor
Open source implementation of AsciiDoc in Ruby
= Document Title
Doc Writer <doc@asciidoc.org>
v1.0, 2013-01-01: Initial version
http://asciidoc.org[AsciiDoc] is a lightweight markup language.
This is the optional preamble (an untitled section body), useful for
writing simple sectionless documents consisting only of a preamble.
NOTE: The abstract, preface, appendix, bibliography, glossary and
index section titles are significant (_specialsections_).
== First section
Document sections start at *level 1* and can nest four levels deep.
* Item 1
* Item 2
[source,ruby]
puts 'Hello, World!'$ binary: git merge fix-village
warning: Cannot merge binary files: quijote.doc (HEAD vs. fix-village)
Auto-merging quijote.doc
CONFLICT (content): Merge conflict in quijote.doc
Automatic merge failed; fix conflicts and then commit the result.
$ binary: git diff master..fix-village
diff --git a/quijote.doc b/quijote.doc
index 4d7a47b..c8a6206 100644
Binary files a/quijote.doc and b/quijote.doc differRuby is a dynamic, reflective, object-oriented, general-purpose programming language
Wikipedia
Let’s see some examples
require 'asciidoctor'
class Person
attr_accessor :first, :last, :weight, :height
def initialize(params = {})
@first = params[:first]
@last = params[:last]
@weight = params[:weight]
@height = params[:height]
end
def has_more_lines?
end
end
p = Person.new(
height: 170cm,
weight: 72,
last: 'Doe',
first: 'John'
)def foo(bar, &block)
callback = block
callback.call(bar)
end
foo(5) {|x| x * x} # => 25
{ "x" => 1, "y" => 2, "z" => 3 }.each{ |k,v| puts "Key: #{k}, Value: #{v}" }class Money
attr_accessor :value
def self.method_missing(m, *args, &block)
if method_sym.to_s =~ /^find_by_(.*)$/
end
end
def +(b)
result = dup
result.value += b.value
return result
end
end
Money.findByCurrency(..)module Module1
def self.myMethod
puts "hi"
end
def others
end
end
Module1.myMethodJava implementation of Ruby language
Ruby rubyRuntime = JavaEmbedUtils.initialize(new ArrayList());
RubyRuntimeAdapter evaler = JavaEmbedUtils.newRuntimeAdapter();
RubyInteger rfj = evaler.eval(runtime, "1+2");
evaler.eval(runtime, "puts 'Hello World'");//Ruby block.rb
class Block < AbstractBlock
def initialize(parent, context, opts = {})
end
end
//Java
//Load Ruby script file
String scriptBlockClass = load("block.rb");
evaler.eval(runtime, scriptBlockClass);
//Load class
IRubyObject rubyClass = evaler.eval(runtime, "Block");
String context = ...;
Map<Object, Object> ops = ..;
Object[] parameters = new Object[]{ parent, context, ops };
//Parameters as Java types
JavaEmbedUtils.invokeMethod(runtime, rubyClass, "new", parameters, IRubyObject.class);x.something → x.getSomething() x.something = 42 → x.setSomething(42) x.something? → x.isSomething() x.method_name → x.methodName()
//Ruby
@block_extensions = {:block => class}
def find_block_extension name
@block_extensions[name]
end
//Java
String blockName = "block";
this.asciidoctorModule.find_block_extension(
RubySymbol.newSymbol(rubyRuntime, blockName));//Ruby
require 'java'
require 'asciidoctor'
class AsciidoctorModule
def render_file(content, options = {})
return Asciidoctor.render_file(content, options)
end
def render(content, options = {})
return Asciidoctor.render(content, options)
end
end
//Java
public interface AsciidoctorModuleClass {
Object render(String content, Map<String, Object> options);
Object renderFile(String filename, Map<String, Object> options);
}
Object rfj = evaler.eval(runtime, "AsciidoctorModule.new()");
AsciidoctorModuleClass amc = JavaEmbedUtils.rubyToJava(runtime, (org.jruby.runtime.builtin.IRubyObject) rfj, AsciidoctorModuleClass.class);When I see a bird that walks like a duck and swims like a duck and quacks like a duck.
James Whitcomb
A group of classes to modify content.
require 'asciidoctor'
require 'asciidoctor/extensions'
require 'uri-open'
class UriIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
def handles? target
target.start_with?('http://') or target.start_with?('https://')
end
def process reader, target, attributes
content = open(target).readlines
reader.push_include content, target, target, 1, attributes
end
end
Asciidoctor::Extensions.register do |document|
include_processor UriIncludeProcessor.new
endpublic abstract class IncludeProcessor extends Processor {
public IncludeProcessor() {
this(new HashMap<String, Object>());
}
public IncludeProcessor(Map<String, Object> config) {
super(config);
}
public abstract boolean handles(String target);
public abstract void process(PreprocessorReader reader, String target, Map<String, Object> attributes);
}
extensionProcessor.includeProcessor(new IncludeProcessor(new HashMap<String, Object>()) {
//----
}<dependency>
<groupId>rubygems</groupId>
<artifactId>asciidoctor</artifactId>
<version>${asciidoctor.version}</version>
<type>gem</type>
<scope>provided</scope>
</dependency>
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
<version>1.0.0-rc4</version>
<configuration>
<jrubyVersion>${jruby.version}</jrubyVersion>
<gemHome>${project.build.directory}/classes</gemHome>
<gemPath>${project.build.directory}/classes</gemPath>
</configuration>
<!-- Executions configuration -->
</plugin>wget -O asciidoctor-master.zip https://github.com/asciidoctor/asciidoctor/archive/master.zip
unzip asciidoctor-master.zip
cp install-asciidoctor-gem.pom asciidoctor-master/pom.xml
cd asciidoctor-master
ASCIIDOCTOR_VERSION=`grep 'VERSION' ./lib/asciidoctor/version.rb | sed "s/.*'\(.*\)'.*/\1/"`
sed -i "s;<version></version>;<version>$ASCIIDOCTOR_VERSION</version>;" pom.xml
mvn install
cd ..
rm -rf asciidoctor-master*
mvn test -Dasciidoctor.version=$ASCIIDOCTOR_VERSION
<!-- install-asciidoctor-gem.pom -->
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>gem-maven-plugin</artifactId>
<version>${jruby.plugins.version}</version>
<extensions>true</extensions>
</plugin>